﻿using System;
using System.Collections.Generic;
using System.Net;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using System.Web.UI.HtmlControls;
using System.Collections;
using Newtonsoft.Json;
using System.Linq;

using CE.Skyscanner;

public partial class travel_flights_flights_booking_redirect : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["deeplink"] != null && Request.QueryString["out"] != null && Request.QueryString["in"] != null && Request.QueryString["agent"] != null)
            {
                string _strDeeplink = Request.QueryString["deeplink"].ToString();
                string _strOutBoundLegId = Request.QueryString["out"].ToString();
                string _strInBoundLegId = Request.QueryString["in"].ToString();
                int _intAgentId = int.Parse(Request.QueryString["agent"].ToString());
                Itineraries _objItineraries = new Itineraries();
                _objItineraries.OutboundLegId = _strOutBoundLegId;
                _objItineraries.InboundLegId = _strInBoundLegId;
                _objItineraries.BookingDetailsLink = new BookingDetailsLink();
                _objItineraries.BookingDetailsLink.Uri = _strDeeplink;

                string _strBookingURL = getBookingDetails_step1(_objItineraries, _intAgentId);
                Response.Redirect(_strBookingURL);
            }
        }
    }
    protected string getBookingDetails_step1(Itineraries _objItineraries, int AgentID)
    {
        // Create a request using a URL that can receive a post. 
        WebRequest request = WebRequest.Create("http://partners.api.skyscanner.net" + _objItineraries.BookingDetailsLink.Uri);
        // Set the Method property of the request to POST.
        request.Method = "PUT";
        // Create POST data and convert it to a byte array.
        string postData = "apikey=ce546556254256234475323023578300&outboundlegid=" + _objItineraries.OutboundLegId +
            "&inboundlegid=" + _objItineraries.InboundLegId;
        byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        // Set the ContentType property of the WebRequest.
        request.ContentType = "application/x-www-form-urlencoded";
        // Set the ContentLength property of the WebRequest.
        request.ContentLength = byteArray.Length;
        // Get the request stream.
        Stream dataStream = request.GetRequestStream();
        // Write the data to the request stream.
        dataStream.Write(byteArray, 0, byteArray.Length);
        // Close the Stream object.
        dataStream.Close();
        // Get the response.
        WebResponse response = request.GetResponse();
        // Display the status.
        //Console.WriteLine(((HttpWebResponse)response).StatusDescription);

        response.Close();

        return getBookingDetails_step2(response.Headers["Location"], AgentID);
    }
    protected string getBookingDetails_step2(string _strResponseURL, int AgentID)
    {
        string result = "";
        try
        {
            SkyscannerDataInfo _objSkyscannerDataInfo = new SkyscannerDataInfo();

            string getData = "?apikey=ce546556254256234475323023578300";

            WebRequest request = null;
            request = WebRequest.Create(_strResponseURL + getData);
            request.Method = "GET";
            WebResponse response = request.GetResponse();
            // Get the stream containing content returned by the server.
            Stream dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd();
            //// Display the content.
            //Label1.Text = responseFromServer;
            // Clean up the streams.
            reader.Close();
            dataStream.Close();

            response.Close();

            BookingDetails _objBookingDetails = JsonConvert.DeserializeObject<BookingDetails>(responseFromServer);
            foreach (BookingOptions item in _objBookingDetails.BookingOptions)
            {
                if (item.BookingItems[0].AgentID == AgentID)
                    result = item.BookingItems[0].Deeplink;
            }
        }
        catch (Exception ex)
        {

        }
        return result;
    }
}
